package com.sali.rule;


import com.sali.utils.IpUtil;

/**
 * @desc 数值IP转字符串IP
 */
public class NumIpToStrIp implements Rule {
    private static Rule rule;

    static {
        rule = new NumIpToStrIp();
    }

    @Override
    public Object invoke(Object ipStr) {
        if (ipStr instanceof Long) {
            return IpUtil.ip2Str((Long) ipStr);
        } else if (ipStr instanceof String) {
            try {
                long ip = Long.parseLong((String) ipStr);
                return IpUtil.ip2Str(ip);
            } catch (Exception e) {
                return ipStr;
            }
        } else {
            return ipStr;
        }
    }

    /**
     * 获取实例方法
     *
     * @return
     */
    public static Rule getInstance() {
        return rule;
    }

    public static void main(String[] args) {
        Rule instance = NumIpToStrIp.getInstance();
        Object invoke = instance.invoke("127.0.0.1");
        System.out.println(invoke);
    }
}
